package com.zb.network.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.zb.entity.FieldContentVo;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjNetwork;
import com.zb.network.service.ConNetworkAuthenFieldService;
import com.zb.result.PageUtil;
import com.zb.service.ZjNetworkAloneAuthenFieldService;
import com.zb.service.ZjNetworkEmpAuthenFieldService;
import com.zb.service.ZjNetworkService;
import com.zb.service.ZjNetworkTeamAuthenFieldService;
import com.zb.vo.NetworkAuthenFieldSelectVo;
import com.zb.vo.NetworkAuthenFieldVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author WEN
 * @Date 2024/1/31 0031
 **/
@Service
public class ConNetworkAuthenFieldServiceImpl implements ConNetworkAuthenFieldService {
    @Resource
    private ZjNetworkTeamAuthenFieldService networkTeamAuthenFieldService;
    @Resource
    private ZjNetworkAloneAuthenFieldService networkAloneAuthenFieldService;
    @Resource
    private ZjNetworkEmpAuthenFieldService networkEmpAuthenFieldService;
    @Resource
    private ZjNetworkService networkService;

    // 保存或更新认证字段
    @Override
    public void save(Long custAccId, Integer type, NetworkAuthenFieldVo authenFieldVo) {
        if (authenFieldVo.getType() == 3 || authenFieldVo.getType() == 4) {
            Assert.isTrue(CollectionUtil.isNotEmpty(authenFieldVo.getContents()), "单选或多选类型的内容不能为空");
            Map<String, Boolean> existName = new HashMap<>();
            for (FieldContentVo vo : authenFieldVo.getContents()) {
                Assert.isNull(existName.get(vo.getVal().trim()), "单选或多选的内容已重复："+ vo.getVal());
                existName.put(vo.getVal().trim(), true);
            }
        }
        // 服务商团队
        if (type == 1) {
            networkTeamAuthenFieldService.save(custAccId, authenFieldVo);
            return;
        }
        // 服务商个人
        if (type == 2) {
            networkAloneAuthenFieldService.save(custAccId, authenFieldVo);
            return;
        }
        // 工程师
        networkEmpAuthenFieldService.save(custAccId, authenFieldVo);
    }

    // 获取认证字段列表
    @Override
    public Map<String, Object> getList(Long custAccId, Integer type, SelectVo selectVo) {
        if (type == 1) {
            return networkTeamAuthenFieldService.getList(custAccId, selectVo);
        }
        if (type == 2) {
            return networkAloneAuthenFieldService.getList(custAccId, selectVo);
        }
        return networkEmpAuthenFieldService.getList(custAccId, selectVo);
    }

    // 根据认证字段ID获取认证字段详情
    @Override
    public NetworkAuthenFieldVo getDetail(Integer type, Long id) {
        if (type == 1) {
            return networkTeamAuthenFieldService.getDetail(id);
        }
        if (type == 2) {
            return networkAloneAuthenFieldService.getDetail(id);
        }
        return networkEmpAuthenFieldService.getDetail(id);
    }

    // 批量启用或禁用认证字段
    @Override
    public void updateDisabledBatchById(Integer type, List<Long> ids, boolean status) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        if (type == 1) {
            networkTeamAuthenFieldService.updateDisabledBatchById(ids, status);
            return;
        }
        if (type == 2) {
            networkAloneAuthenFieldService.updateDisabledBatchById(ids, status);
            return;
        }
        networkEmpAuthenFieldService.updateDisabledBatchById(ids, status);
    }

    // 批量删除认证字段
    @Override
    public void deleteBatchById(Integer type, List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        Assert.isTrue(false, "正在调试中，请勿操作....");
        if (type == 1) {
            networkTeamAuthenFieldService.deleteBatchById(ids);
            return;
        }
        if (type == 2) {
            networkAloneAuthenFieldService.deleteBatchById(ids);
            return;
        }
        networkEmpAuthenFieldService.deleteBatchById(ids);
    }

    // 获取服务商认证字段
    @Override
    public List<NetworkAuthenFieldSelectVo> getNetworkAuthenField(Integer type, Long custAccId) {
        if (type == 1) {
            return networkTeamAuthenFieldService.getNetworkAuthenField(custAccId);
        }
        if (type == 2) {
            return networkAloneAuthenFieldService.getNetworkAuthenField(custAccId);
        }
        return networkEmpAuthenFieldService.getNetworkAuthenField(custAccId);
    }

}
